package everything.search;
import everything.Flag;

public class AgentState implements Comparable<AgentState>{
	private int _x;
	private int _y;
	private String _orientation;
	private int _bullets;
	private int _xOpponent;
	private int _yOpponent;
	private String _dOpponent;
	private int _bOpponent;
	private boolean _health;
	private Flag[] _flags;
	private int _remainingMoves;
	private boolean _hOpponent;

	// constructor
	public AgentState(int x, int y, String orientation, int bullets, boolean health, int xOpponent, int yOpponent, String dOpponent, int bOpponent, boolean hOpponent,Flag[] flags, int remainingMoves){
		this._health = health;
		this._bOpponent = bOpponent;
		this._dOpponent = dOpponent;
		this._xOpponent = xOpponent;
		this._yOpponent = yOpponent;
		this._hOpponent = hOpponent;
		this._bullets = bullets;
		this._orientation = orientation;
		this._x = x;
		this._y = y;
		this._flags = flags;
		this._remainingMoves = remainingMoves;
	}

	public int getX() {
		return _x;
	}
	public int getY() {
		return _y;
	}
	public String getOrientation() {
		return _orientation;
	}

	public int getBullets(){
		return _bullets;
	}

	public boolean getHealth() {
		return _health;
	}

	public int getXOpponent() {
		return _xOpponent;
	}
	public int getYOpponent() {
		return _yOpponent;
	}
	public String getDOpponent() {
		return _dOpponent;
	}
	public int getBOpponent() {
		return _bOpponent;
	}

	public boolean getHOpponent() {
		return _hOpponent;
	}

	public boolean isTerminal(){
		boolean flag = false;
		for (int i=0; i<_flags.length; i++){
			if (_flags[i].getOwner()==0){
				flag = true;
				break;
			}
		}
		return _remainingMoves<=0 || !flag;
	}

	@Override
	public String toString(){
		String ans = "Me: ["+_x+","+_y+"] -<"+_orientation+"> "+ (_health ?  "A" : "Dead") + " B: " + _bullets +
				" Op: ["+_xOpponent+","+_yOpponent+"] -<"+_dOpponent+"> " + (_hOpponent ?  "A" : "Dead") + " B: " + _bOpponent
				+ " Moves " +_remainingMoves+ " Flags ";
		for (int i = 0; i < _flags.length; i++){
			ans = ans +" " + _flags[i].getOwner();
		}

		return ans;
		}

	public Flag[] getFlags() {
		return _flags;
	}
	@Override
	public boolean equals(Object o){
		if (o instanceof AgentState){
			AgentState other = (AgentState)o;
			System.out.println(this + "  -  " + other);
			if (this._x != other._x) return false;
			if (this._y != other._y) return false;
			if (this._bullets != other._bullets) return false;
			if (this._remainingMoves != other._remainingMoves) return false;
			if (this._xOpponent != other._xOpponent) return false;
			if (this._yOpponent != other._yOpponent) return false;
			if (this._bOpponent != other._bOpponent) return false;
			if (this._dOpponent != null && other._dOpponent != null){
				if(!this._dOpponent.equals(other._dOpponent)) return false;
			} else if (other._dOpponent != null){
				return false;
			} else if (this._dOpponent != null) {
				return false;
			}
			if (this._orientation != null && other._orientation !=null){
				if(!this._orientation.equals(other._orientation)) return false;
			} else if (other._orientation != null ){
				return false;
			} else if (this._orientation != null){
				return false;
			}
			if (this._health != other._health) return false;
			if (this._hOpponent != other._hOpponent) return false;
			for (int i=0; i<this._flags.length; i++){
				if (this._flags[i] != other._flags[i]) return false;
			}
			return true;
		} else return false;

	}

//	@Override
	public int compareTo(AgentState other) {
		if (this._x > other._x) return -1;
		else if (this._x < other._x) return 1;
		if (this._y > other._y) return 1;
		if (this._y < other._y) return -1;
		if (this._bullets > other._bullets) return 1;
		if (this._bullets < other._bullets) return -1;
		if (this._remainingMoves > other._remainingMoves) return 1;
		if (this._remainingMoves < other._remainingMoves) return -1;
		if (this._xOpponent > other._xOpponent) return 1;
		if (this._xOpponent < other._xOpponent) return -1;
		if (this._yOpponent > other._yOpponent) return 1;
		if (this._yOpponent < other._yOpponent) return -1;
		if (this._bOpponent > other._bOpponent) return 1;
		if (this._bOpponent < other._bOpponent) return -1;
		if (this._dOpponent != null && other._dOpponent !=null){
			int tmp = this._dOpponent.compareTo(other._dOpponent);
			if (tmp!=0) return tmp;
		} else if (other._dOpponent!=null){
			return -1;
		} else if (this._dOpponent!=null){
			return 1;
		}
		if (this._orientation != null && other._orientation != null){
				int tmp = this._orientation.compareTo(other._orientation);
				if (tmp!=0) return tmp;
		} else if (other._orientation!=null){
			return -1;
		} else if (this._orientation!=null){
			return 1;
		}
		if (this._health){
			if (!other._health) return -1;
		} else if (other._health) return 1;
		if (this._hOpponent){
			if (!other._hOpponent) return -1;
		} else if (other._hOpponent) return 1;
		for (int i=0; i < _flags.length; i++){
			if (this._flags[i].getOwner() > other._flags[i].getOwner()) return 1;
			if (this._flags[i].getOwner() < other._flags[i].getOwner()) return -1;
		}
		return 0;
	}

	public int getRemainingMoves() {
		return _remainingMoves;
	}
}